// JavaScript Document
var pathToSearchFile = "sphider/search.php";

$(document).ready(function(){
	$("#searchbutton").click(function(event){
	event.preventDefault();
//	if(document.getElementById&&document.getElementsByTagName&&document.createElement){ 

		var searchbutton=$('searchbutton');

		if(searchbutton){	
			sendHttpRequest(pathToSearchFile+'?search=1&query='+$('#searchinput').val(),'displayResults'); 
		} 
//	} 
});
});




// send http requests 

function sendHttpRequest(url,callbackFunc,respXml){ 

	var xmlobj=null; 
	
	try{ 
		xmlobj=new XMLHttpRequest(); 
	} 
	catch(e){ 
		try{ 
			xmlobj=new ActiveXObject("Microsoft.XMLHTTP"); 
		} 
		catch(e){ 
			alert('AJAX is not supported by your browser!'); 
			return false; 
		} 
	} 
	xmlobj.onreadystatechange=function(){

		if(xmlobj.readyState===4){ 

			if(xmlobj.status===200){ 

				respXml?eval(callbackFunc+'(xmlobj.responseXML)'):eval(callbackFunc+'(xmlobj.responseText)'); 
			} 
		} 
	};

	// open socket connection 

	xmlobj.open('GET',url,true); 
	// send http header 
	xmlobj.setRequestHeader('Content-Type','text/html;charset=ISO-8859-1'); 
	// send http request	
	xmlobj.send(null); 
} 


// display search results 
function displayResults(results){ 
	var rescontainer=$("#resultcontainer"); 
	if(!rescontainer){
		return;
		}

	results = results.slice(results.search('"result_report"')+16);
	results = results.substring(0,results.search('</div>'));
	rescontainer.html(results);

} 



function onEnter( event, form ) { 
	var keyCode = null; 
	
	if( event.which ) { 
		keyCode = event.which; 
	} else if(event.keyCode){ 
		keyCode = event.keyCode; 
	} 
	if( 13 === keyCode ) { 
		form.search.click(); 
		return false; 
	} 
	return true; 
}